import { getcangkulist, getlistWZZY, getlistZGDW, getlistZBDJ, getCKlxlist, getWzqf, getTuoPanlist, getTuoPanALL, getHuoWeiALL, getHuoWeilist, getKflist, getYhKflist } from '@/api/biaozhun'
import store from '@/store'
import axios from 'axios'
export default {
  /**
   * @description 格式化时间
   * @param time
   * @param cFormat
   * @returns {string|null}
   */
  parseTime(time, cFormat) {
    if (arguments.length === 0) {
      return null
    }
    const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}'
    let date
    if (typeof time === 'object') {
      date = time
    } else {
      if (typeof time === 'string' && /^[0-9]+$/.test(time)) {
        time = parseInt(time)
      }
      if (typeof time === 'number' && time.toString().length === 10) {
        time = time * 1000
      }
      date = new Date(time)
    }
    const formatObj = {
      y: date.getFullYear(),
      m: date.getMonth() + 1,
      d: date.getDate(),
      h: date.getHours(),
      i: date.getMinutes(),
      s: date.getSeconds(),
      a: date.getDay(),
    }
    const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
      let value = formatObj[key]
      if (key === 'a') {
        return ['日', '一', '二', '三', '四', '五', '六'][value]
      }
      if (result.length > 0 && value < 10) {
        value = '0' + value
      }
      return value || 0
    })
    return time_str
  },

  /**
   * @description 格式化时间
   * @param time
   * @param option
   * @returns {string}
   */
  formatTime(time, option) {
    if (('' + time).length === 10) {
      time = parseInt(time) * 1000
    } else {
      time = +time
    }
    const d = new Date(time)
    const now = Date.now()

    const diff = (now - d) / 1000

    if (diff < 30) {
      return '刚刚'
    } else if (diff < 3600) {
      // less 1 hour
      return Math.ceil(diff / 60) + '分钟前'
    } else if (diff < 3600 * 24) {
      return Math.ceil(diff / 3600) + '小时前'
    } else if (diff < 3600 * 24 * 2) {
      return '1天前'
    }
    if (option) {
      return parseTime(time, option)
    } else {
      return (
        d.getMonth() +
        1 +
        '月' +
        d.getDate() +
        '日' +
        d.getHours() +
        '时' +
        d.getMinutes() +
        '分'
      )
    }
  },
  /**
    * @description 格式化标准时间
    * @param time
    * @returns {string}
    */
  changeBZTime(time) {
    if (time) {
      var date = new Date(time)
    } else {
      var date = new Date()
    }
    var y = date.getFullYear()
    var m = date.getMonth() + 1
    m = m < 10 ? '0' + m : m
    var d = date.getDate()
    d = d < 10 ? '0' + d : d
    return y + '-' + m + '-' + d
  },
  /**
    * @description 获取距离当今的时间
    * @param time
    * @returns {string}
    */
  getMoreTime(time) {
    var date = new Date()
    var y = date.getFullYear()
    var m = date.getMonth() + 1
    var d = date.getDate()
    if (time == 'year') {
      y = y + 1
    } else if (time == 'season') {
      y = y + parseInt((m + 3) / 12);
      m = (m + 3) % 12 ? (m + 3) % 12 : 12;
      d = new Date(y, m, 0).getDate();
    } else if (time == 'month') {
      console.log(y, m, d);
      if (m == 11) {
        y = y;
        m = m + 1;
        d = new Date(y, m, 0).getDate();
      } else {

        y = y + parseInt((m + 1) / 12);
        m = (m + 1) % 12;
        d = new Date(y, m, 0).getDate();
      }
    } else if (time == 'week') {
      date = new Date(date.getTime() + 7 * 24 * 3600 * 1000)
      y = date.getFullYear()
      m = date.getMonth() + 1
      d = date.getDate()
    }
    m = m < 10 ? '0' + m : m
    d = d < 10 ? '0' + d : d
    return y + '-' + m + '-' + d
  },
  /**
   * @description 生成出入库订单编号
   * @param num
   * @returns {string}
   */
  addPreZero(num, type) {
    let date = new Date();
    let year = date.getFullYear();
    let month = date.getMonth() + 1;
    month = month < 10 ? '0' + month : month
    let day = date.getDate();
    day = day < 10 ? '0' + day : day
    return type + year.toString() + month.toString() + day.toString() + ('0000' + Number(num + 1)).slice(-4);
  },
  /**
   * @description 将url请求参数转为json格式
   * @param url
   * @returns {{}|any}
   */
  paramObj(url) {
    const search = url.split('?')[1]
    if (!search) {
      return {}
    }
    return JSON.parse(
      '{"' +
      decodeURIComponent(search)
        .replace(/"/g, '\\"')
        .replace(/&/g, '","')
        .replace(/=/g, '":"')
        .replace(/\+/g, ' ') +
      '"}'
    )
  },

  /**
   * @description 父子关系的数组转换成树形结构数据
   * @param data
   * @returns {*}
   */
  translateDataToTree(data) {
    const parent = data.filter(
      (value) => value.shangjiid === 'undefined' || value.shangjiid == '0'
    )
    const children = data.filter(
      (value) => value.shangjiid !== 'undefined' && value.shangjiid != '0'
    )
    const translator = (parent, children) => {
      parent.forEach((parent) => {
        children.forEach((current, index) => {
          if (current.shangjiid === parent.id) {
            const temp = JSON.parse(JSON.stringify(children))
            temp.splice(index, 1)
            translator([current], temp)
            typeof parent.children !== 'undefined'
              ? parent.children.push(current)
              : (parent.children = [current])
          }
        })
      })
    }
    translator(parent, children)
    return parent
  },

  /**
   * @description 树形结构数据转换成父子关系的数组
   * @param data
   * @returns {[]}
   */
  translateTreeToData(data) {
    const result = []
    data.forEach((item) => {
      const loop = (data) => {
        result.push({
          id: data.id,
          name: data.name,
          parentId: data.parentId,
        })
        const child = data.children
        if (child) {
          for (let i = 0; i < child.length; i++) {
            loop(child[i])
          }
        }
      }
      loop(item)
    })
    return result
  },
  /**
   * @description 递归获取子节点
   * @param id
   * @param array
   * @returns {*}
   */
  getchilds(id, array) {
    const childs = []
    for (const arr of array) {
      if (arr.shangjiid == id) {
        childs.push({
          id: arr.id,
          zhuanyemc: arr.zhuanyemc,
          shangjiid: arr.shangjiid
        })
      }
    }
    for (const child of childs) {
      const childscopy = this.getchilds(child.id, array)// 递归获取子节点
      if (childscopy.length > 0) {
        child.children = childscopy
      }
    } //循环获取子节点if (arr.parentld === id)
    return childs
  },
  /**
   * @description 递归获取父节点
   * @param id
   * @param array
   * @returns {*}
   */
  generateOptions(params) {
    var result = []
    for (const param of params) {
      if (param.shangjiid == 0) {
        var parent = {
          id: param.id,
          zhuanyemc: param.zhuanyemc,
          shangjiid: param.shangjiid,
        }
        parent.children = this.getchilds(param.id, params)
        result.push(parent)
      }
    }
    return result
  },


  /**
   * @description 10位时间戳转换
   * @param time
   * @returns {string}
   */
  tenBitTimestamp(time) {
    const date = new Date(time * 1000)
    const y = date.getFullYear()
    let m = date.getMonth() + 1
    m = m < 10 ? '' + m : m
    let d = date.getDate()
    d = d < 10 ? '' + d : d
    let h = date.getHours()
    h = h < 10 ? '0' + h : h
    let minute = date.getMinutes()
    let second = date.getSeconds()
    minute = minute < 10 ? '0' + minute : minute
    second = second < 10 ? '0' + second : second
    return y + '年' + m + '月' + d + '日 ' + h + ':' + minute + ':' + second //组合
  },

  /**
   * @description 13位时间戳转换
   * @param time
   * @returns {string}
   */
  thirteenBitTimestamp(time) {
    const date = new Date(time / 1)
    const y = date.getFullYear()
    let m = date.getMonth() + 1
    m = m < 10 ? '' + m : m
    let d = date.getDate()
    d = d < 10 ? '' + d : d
    let h = date.getHours()
    h = h < 10 ? '0' + h : h
    let minute = date.getMinutes()
    let second = date.getSeconds()
    minute = minute < 10 ? '0' + minute : minute
    second = second < 10 ? '0' + second : second
    return y + '-' + m + '-' + d //组合
  },

  /**
   * @description 获取随机id
   * @param length
   * @returns {string}
   */
  uuid(length = 32) {
    const num = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'
    let str = ''
    for (let i = 0; i < length; i++) {
      str += num.charAt(Math.floor(Math.random() * num.length))
    }
    return str
  },
  //过期时间
  expireTime(data, number) {
    if (data) {
      let newdata = new Date(data);
      let month = newdata.getMonth() + 1;
      var targetMonth = month
      if (number) {
        targetMonth += Number(number);   // 目标月份
      }
      // console.log(targetMonth);
      var targetYear = newdata.getFullYear();  // 目标年份
      while (targetMonth > 12) {// 如果月份大于12，需要增加一年
        targetMonth -= 12;      // 更新月份
        targetYear++;           // 更新年份
      }
      newdata.setMonth(targetMonth - 1);     // 更新日期对象中的月份
      newdata.setFullYear(targetYear);       // 更新日期对象中的年份
      let result = newdata.getFullYear() + '-' + (newdata.getMonth() + 1) + '-' + newdata.getDate();
      return result
    }
    return
  },
  //文件上传
  validFile(file, type) {
    if (!file || !file.name || file.name.indexOf('.') < 0) return false
    let reg; const ext = file.name.split('.').pop()
    if (type == 'excel') { // excel格式
      reg = ['xls', 'csv', 'xlsx', 'wps']
    } else { // 图片格式
      reg = ['jpeg', 'jpg', 'png', 'bmp', 'gif']
    }
    return reg.includes(ext.toLowerCase())
  },
  /**
   * @description m到n的随机数
   * @param m
   * @param n
   * @returns {number}
   */
  random(m, n) {
    return Math.floor(Math.random() * (m - n) + n)
  },
  /**
   * 获取当前日期函数
   */
  getNowFormatDate() {
    let date = new Date(),
      year = date.getFullYear(), //获取完整的年份(4位)
      month = date.getMonth() + 1, //获取当前月份(0-11,0代表1月)
      strDate = date.getDate() // 获取当前日(1-31)
    if (month < 10) month = `0${month}` // 如果月份是个位数，在前面补0
    if (strDate < 10) strDate = `0${strDate}` // 如果日是个位数，在前面补0
    return `${year}年${month}月${strDate}日`
  },
  /**
   * 获取物资区分
   */

  getWzqf(id) {
    const data = [
      { id: "", name: "全部" },
      { id: '1', name: "使用回库" },
      { id: '2', name: "首次入库" },
      { id: '3', name: "维修入库" },
    ];
    return id ? (data[id - 1] ? data[id - 1].name : '') : data;
  },
  /**
   * 获取物资区分2
   */

  async getWzqflist(id) {
    let data = store.getters['biaozhun/wuziqflist'];
    if (id) {
      let datarow = data.filter(item => item.id == id);
      return datarow[0].mingcheng;
    }
    let obj = [{ id: "", mingcheng: '全部' }]
    data = [...obj, ...data]
    return data
  },
  /**
   * 获取入库状态
   */
  getRkzt(id) {
    const data = [
      { id: 1, name: "新单待校验" },
      { id: 2, name: "校验待入库" },
      { id: 3, name: "入库完成" }
    ];
    if (id) {
      let datarow = data.filter(item => item.id == id);
      return datarow.length > 0 ? datarow[0].name : '';
    }
    return data
  },
  /**
   * 获取入库类型
   */
  getRklx(id) {
    const data = [
      { id: '1', name: "使用回库" },
      { id: '2', name: "首次入库" },
      { id: '3', name: "维修入库" }
    ];
    if (id) {
      let datarow = data.filter(item => item.id == id);
      return datarow.length > 0 ? datarow[0].name : '';
    }
    return data
  },
  /**
   * 获取托盘类型
   */
  gettplx(id) {
    const data = [
      { id: '1', name: "下架" },
      { id: '2', name: "上架" }
    ];
    if (id) {
      let datarow = data.filter(item => item.id == id);
      return datarow.length > 0 ? datarow[0].name : '';
    }
    return data
  },
  /**
   * 获取库房
   */
  async getKf(cangkuid, id) {
    const { data } = await getKflist({ "pageSize": 99999, "cangkuid": cangkuid })
    if (id) {
      let datarow = data.filter(item => item.id == id);
      return datarow[0].mingcheng;
    }
    return data
  },
  /**
   * 获取用户库房
   */
  async getYHKf(id) {
    const { data } = await getYhKflist({})
    if (id) {
      let datarow = data.filter(item => item.id == id);
      return datarow[0].mingcheng;
    }
    return data
  },
  /**
    * 获取货位
    */
  async getHwAll(id) {
    let { data } = await getHuoWeiALL({})
    if (id) {
      let datarow = data.filter(item => item.id == id);
      return datarow[0].mingcheng;
    }
    return data
  },
  /**
   * 获取货位
   */
  async getHw(id) {
    let { data } = await getHuoWeilist({})
    if (id) {
      let datarow = data.filter(item => item.id == id);
      return datarow[0].mingcheng;
    }
    return data
  },
  /**
   * 获取托盘
   */
  async getTpAll(id) {
    let { data } = await getTuoPanALL({})
    if (id) {
      let datarow = data.filter(item => item.id == id);
      return datarow[0].tuopanh;
    }
    return data
  },
  /**
  * 获取托盘
  */
  async getTp(id) {
    let { data } = await getTuoPanlist({})
    if (id) {
      let datarow = data.filter(item => item.id == id);
      return datarow[0].tuopanh;
    }
    return data
  },
  /**
   * 获取主管单位  getlistZGDW
   */
  getZgdw(id) {
    let data = store.getters['biaozhun/zgdwlist'];
    if (id) {
      let datarow = data.filter(item => item.id == id);
      return datarow[0].danweimc;
    }
    let obj = [{ id: "", danweimc: '全部' }]
    data = [...obj, ...data]
    return data
  },
  /**
   * 获取物资分类  getlistWZZY
   */
  async getWzzy(id) {
    let data = store.getters['biaozhun/wuzizylist'];
    if (id) {
      let datarow = data.filter(item => item.zhuanyeid == id);
      return datarow[0].zhuanyemc;
    }
    let obj = [{ id: "", zhuanyemc: '全部' }]
    data = [...obj, ...data]
    return data
  },

  /**
   * 获取上架状态
   */
  getSjzt(id) {
    const data = [
      { id: 0, name: "验收" },
      { id: 1, name: "未开始" },
      { id: 2, name: "进行中" },
      { id: 3, name: "入库完成" },
      { id: 4, name: "扫码确认" },
    ];
    if (id) {
      let datarow = data.filter(item => item.id == id);
      return datarow.length > 0 ? datarow[0].name : '';
    }
    return data
  },

  /**
   * 获取上级名称
   */
  getSjjg(id) {
    const data = [
      { id: 1, name: "上级1" },
      { id: 2, name: "上级2" },
      { id: 3, name: "上级3" },
      { id: 4, name: "上级4" }
    ];
    return id ? (data[id - 1] ? data[id - 1].name : '') : data;
  },
  /**
   * 获取机构类别
   */
  getJglb(id) {
    const data = [
      { id: 1, name: "机构1" },
      { id: 2, name: "机构2" },
      { id: 3, name: "机构3" },
      { id: 4, name: "机构4" }
    ];
    return id ? (data[id - 1] ? data[id - 1].name : '') : data;
  },
  /**
   * 获取公共仓库列表
   */
  async getCklist(id) {
    let { data } = await getcangkulist({})
    if (id) {
      let datarow = data.filter(item => item.id == id);
      return datarow[0].mingcheng;
    }
    return data
  },
  /**
   * 获取公共 储备等级 列表
   */
  async getZBDJlist(id) {
    let { data } = await getlistZBDJ({})
    data.forEach(function (element) {
      element.value = parseInt(element.id)
    });
    if (id) {
      let datarow = data.filter(item => item.id == id);
      return datarow[0].mingcheng;
    }

    return data
  },
  /**
   * 获取出库类型
   */
  getCKlxlist(id) {
    let data = store.getters['biaozhun/chukulxlist'];
    if (id) {
      let datarow = data.filter(item => item.id == id);
      return datarow[0].mingcheng;
    }
    let obj = [{ id: "", mingcheng: '全部' }]
    data = [...obj, ...data]
    return data
  },
  /**
   * 获取出库类型
   */
  getCklx(id) {
    const data = [
      { id: "1", name: "使用出库" },
      { id: "2", name: "维修出库" },
      { id: "3", name: "报损出库" }
    ];
    if (id) {
      let datarow = data.filter(item => item.id == id);
      console.log(id, datarow);
      return datarow.length > 0 ? datarow[0].name : '';
    }
    return data
  },
  /**
   * 获取出库状态
   */
  getCkzt(id) {
    const data = [
      { id: 1, name: "未开始" },
      { id: 2, name: "进行中" },
      { id: 3, name: "出库完成" }
    ];
    if (id) {
      let datarow = data.filter(item => item.id == id);
      return datarow.length > 0 ? datarow[0].name : '';
    }
    return data
  },
  /**
   * 获取货位状态
   */
  getHwzt(id) {
    const data = [
      { id: '1', name: "空闲", color: '#AB1B1B' },
      { id: '2', name: "占用", color: 'linear-gradient(to top, #00C6DD 0%,#00C6DD 50%,transparent 50%,transparent 100%)' },
      { id: '3', name: "锁定", color: '#909399' },
      { id: '4', name: "异常", color: '#909399' },
      { id: '5', name: "禁用", color: '#909399' }
    ];
    return id != undefined ? (data[id] ? data[id].name : '') : data;
  },
  /**
   * 获取货位类型
   */
  getHwlx(id) {
    const data = [
      { id: 1, name: "A型货位" },
      { id: 2, name: "B型货位" }
    ];
    return id != undefined ? (data[id] ? data[id].name : '') : data;
  },
  /**
   * 获取出入库类型
   */
  getrzcrk(id) {
    const data = [
      { id: '1', name: "入库" },
      { id: '2', name: "出库" },
      { id: '3', name: "维修出库" },
      { id: '4', name: "盘点出库" },
      { id: '5', name: "报损出库" }
    ];
    if (id) {
      let datarow = data.filter(item => item.id == id);
      return datarow.length > 0 ? datarow[0].name : '';
    }
  },
  /**
   * 获取出库调拨单状态
   */
  getCkdbdZt(id) {
    const data = [
      { id: "", name: "全部" },
      { id: '0', name: "新单" },
      { id: '1', name: "待审核" },
      { id: '2', name: "审核驳回" },
      { id: '3', name: "审核通过待分发" },
      { id: '5', name: "分发完成" },
      { id: '15', name: "出库成功" }
    ];
    if (id) {
      let datarow = data.filter(item => item.id == id);
      return datarow.length > 0 ? datarow[0].name : '';
    }
    return data
  },
  /**
   * 获取报废单状态
   */
  getBfdzt(id) {
    const data = [
      { id: "", name: "全部" },
      { id: '0', name: "新单待上报" },
      { id: '5', name: "新单待审批" },
      { id: '10', name: "审批通过" },
      { id: '15', name: "审批未通过" }
    ];
    if (id) {
      let datarow = data.filter(item => item.id == id);
      return datarow.length > 0 ? datarow[0].name : '';
    }
    return data
  },
  /**
   * 获取报废任务单状态
   */
  getBfrwdzt(id) {
    const data = [
      { id: 0, name: "验收" },
      { id: 1, name: "未开始" },
      { id: 2, name: "进行中" },
      { id: 3, name: "报损完成" },
      { id: 4, name: "扫码确认" },
    ];
    if (id) {
      let datarow = data.filter(item => item.id == id);
      return datarow.length > 0 ? datarow[0].name : '';
    }
    return data
  },
  /**
   * 获取报废单状态
   */
  getJhdzt(id) {
    const data = [
      { id: '1', name: "未开始" },
      { id: '2', name: "进行中" },
      { id: '3', name: "出库完成" }
    ];
    if (id) {
      let datarow = data.filter(item => item.id == id);
      return datarow.length > 0 ? datarow[0].name : '';
    }
    return data
  },
  /**
   * 获取报损单状态
   */
  getbsdzt(id) {
    const data = [
      { id: 1, name: "新单待报损" },
      { id: 2, name: "进行中" },
      { id: 3, name: "报损完成" },
    ];
    if (id) {
      let datarow = data.filter(item => item.id == id);
      return datarow.length > 0 ? datarow[0].name : '';
    }
    return data
  },
  getwxdzt(id) {
    const data = [
      { id: 1, name: "新单待维修" },
      { id: 2, name: "进行中" },
      { id: 3, name: "维修完成" },
    ];
    if (id) {
      let datarow = data.filter(item => item.id == id);
      return datarow.length > 0 ? datarow[0].name : '';
    }
    return data
  },
  getpddzt(id) {
    const data = [
      { id: 1, name: "新单待盘点" },
      { id: 2, name: "进行中" },
      { id: 3, name: "盘点完成" },
    ];
    if (id) {
      let datarow = data.filter(item => item.id == id);
      return datarow.length > 0 ? datarow[0].name : '';
    }
    return data
  },
  getdbdzt(id) {
    const data = [
      { id: 1, name: "新单待调拨" },
      { id: 2, name: "进行中" },
      { id: 3, name: "调拨完成" },
    ];
    if (id) {
      let datarow = data.filter(item => item.id == id);
      return datarow.length > 0 ? datarow[0].name : '';
    }
    return data
  },
  /**
   * 获取维修任务单状态
   */
  getwxrwdzt(id) {
    const data = [
      { id: 0, name: "验收" },
      { id: 1, name: "未开始" },
      { id: 2, name: "进行中" },
      { id: 3, name: "维修完成" },
      { id: 4, name: "扫码确认" },
    ];
    if (id) {
      let datarow = data.filter(item => item.id == id);
      return datarow.length > 0 ? datarow[0].name : '';
    }
    return data
  },
  /**
   * 获取盘点单状态
   */
  getpdrwdzt(id) {
    const data = [
      { id: 0, name: "验收" },
      { id: 1, name: "未开始" },
      { id: 2, name: "进行中" },
      { id: 3, name: "盘点完成" },
      { id: 4, name: "扫码确认" },
    ];
    if (id) {
      let datarow = data.filter(item => item.id == id);
      return datarow.length > 0 ? datarow[0].name : '';
    }
    return data
  },
  /**
   * 获取调拨任务单状态
   */
  getdbrwdzt(id) {
    const data = [
      { id: 0, name: "验收" },
      { id: 1, name: "未开始" },
      { id: 2, name: "进行中" },
      { id: 3, name: "调拨完成" },
      { id: 4, name: "扫码确认" },
    ];
    if (id) {
      let datarow = data.filter(item => item.id == id);
      return datarow.length > 0 ? datarow[0].name : '';
    }
    return data
  },
  /**
   * 获取挂架状态
   */
  getguajiazt(id) {
    const data = [
      { id: 1, name: "未入库" },
      { id: 2, name: "入库" },
      { id: 3, name: "使用出库" },
      { id: 4, name: "维修出库" },
      { id: 5, name: "盘点出库" },
      { id: 6, name: "报损" },
    ];
    if (id) {
      let datarow = data.filter(item => item.id == id);
      return datarow.length > 0 ? datarow[0].name : '';
    }
    return data
  },
  /**
   * 获取挂架位置
   */
  getguajiawz(id) {
    const data = [
      { id: 1, name: "装机" },
      { id: 2, name: "返修中" },
      { id: 3, name: "在库" },
      { id: 4, name: "驻训" },
    ];
    if (id) {
      let datarow = data.filter(item => item.id == id);
      return datarow.length > 0 ? datarow[0].name : '';
    }
    return data
  },
  /**
   * 获取挂架操作状态
   */
  getguajiaczzt(id) {
    const data = [
      { id: 1, name: "未绑定货位" },
      { id: 2, name: "已绑定货位" },
      { id: 3, name: "待解绑货位" },
    ];
    if (id) {
      let datarow = data.filter(item => item.id == id);
      return datarow.length > 0 ? datarow[0].name : '';
    }
    return data
  },
  /**
   * 获取货架性质
   */
  gethuojiaxz(id) {
    const data = [
      { id: 1, name: "L类型" },
      { id: 2, name: "S类型" },
    ];
    if (id) {
      let datarow = data.filter(item => item.id == id);
      return datarow.length > 0 ? datarow[0].name : '';
    }
    return data
  },
  findNodeById(arr, id) {
    for (let i = 0; i < arr.length; i++) {
      let node = arr[i];
      if (node.id === id) return node;
      if (node.childList && node.childList.length > 0) {
        // 如果当前节点有子节点，则递归查找子节点
        let foundNode = this.findNodeById(node.childList, id);
        if (foundNode) return foundNode;
      }
    }
  },
  // 递归判断列表，把最后的children设为undefined
  getTreeData(data) {
    for (var i = 0; i < data.length; i++) {
      if (data[i].childList.length == 0) {
        // children若为空数组，则将children设为undefined
        data[i].childList = undefined;
      } else {
        // children若不为空数组，则继续 递归调用 本方法
        this.getTreeData(data[i].childList);
      }
    }
    return data;
  }
}

/**
  * @description addEventListener
  * @type {function(...[*]=)}
  */
export const on = (function () {
  return function (element, event, handler, useCapture = false) {
    if (element && event && handler) {
      element.addEventListener(event, handler, useCapture)
    }
  }
})()
/**
   * @description removeEventListener
   * @type {function(...[*]=)}
   */
export const off = (function () {
  return function (element, event, handler, useCapture = false) {
    if (element && event) {
      element.removeEventListener(event, handler, useCapture)
    }
  }
})()
